import { setupLayouts } from 'virtual:meta-layouts';
import generatedRoutes from 'virtual:generated-pages';
import type { RouteRecordRaw } from 'vue-router';
import type { Route } from '#/global';
import useSettingsStore from '@/store/modules/settings';

function Layout() {
  return import('@/layouts/index.vue');
}

// 固定路由（默认路由）
const constantRoutes: RouteRecordRaw[] = [
  {
    path: '/login',
    name: 'login',
    component: () => import('@/views/login.vue'),
    meta: {
      title: '登录',
    },
  },
  {
    path: '/login2',
    name: 'login2',
    component: () => import('@/views/login2.vue'),
    meta: {
      title: '登录',
    },
  },
  {
    path: '/xy/1',
    name: 'xy1',
    component: () => import('@/page/xy/xy1.vue'),
    meta: {
      title: '青少年守护者平台隐私政策',
    },
  },
  {
    path: '/xy/2',
    name: 'xy2',
    component: () => import('@/page/xy/xy2.vue'),
    meta: {
      title: '青少年守护者平台用户服务协议',
    },
  },
  {
    path: '/:all(.*)*',
    name: 'notFound',
    component: () => import('@/views/[...all].vue'),
    meta: {
      title: '找不到页面',
    },
  },
];

// 系统路由
const systemRoutes: RouteRecordRaw[] = [
  {
    path: '/',
    component: () => import('@/layouts/index.vue'),
    meta: {
      title: () => useSettingsStore().settings.home.title,
      breadcrumb: false,
    },
    children: [
      {
        path: '',
        component: () => import('@/views/index.vue'),
        meta: {
          title: () => useSettingsStore().settings.home.title,
          icon: 'i-ant-design:home-twotone',
          breadcrumb: false,
        },
      },
      {
        path: 'reload',
        name: 'reload',
        component: () => import('@/views/reload.vue'),
        meta: {
          title: '重新加载',
          breadcrumb: false,
        },
      },
    ],
  },
];

// 动态路由（异步路由、导航栏路由）
const asyncRoutes: Route.recordMainRaw[] = [
  {
    meta: {
      title: '菜单1',
      icon: 'i-uim:box',
    },
    children: [
      {
        path: '/organization',
        component: Layout,
        redirect: '/organization/list',
        meta: {
          title: '机构管理',
          icon: 'i-heroicons-solid:menu-alt-3',
        },
        children: [
          {
            path: 'list',
            component: () => import('@/page/organization/list.vue'),
            meta: {
              title: '机构管理',
            },
          },
          {
            path: 'info',
            name: 'viewOrganization',
            component: () => import('@/page/organization/info.vue'),
            meta: {
              title: '机构详情',
              menu: false,
              activeMenu: '/organization/list',
            },
          },
          {
            path: 'people',
            component: () => import('@/page/organization/people/index.vue'),
            meta: {
              title: '机构人员',
            },
            children: [
              {
                path: 'editTeacher',
                name: 'editTeacher',
                component: () =>
                  import('@/page/organization/people/TeacherForm.vue'),
                meta: {
                  title: '教师列表',
                  menu: false,
                  activeMenu: '/organization/people',
                },
              },
              {
                path: 'importTeacher',
                name: 'importTeacher',
                component: () =>
                  import('@/page/organization/people/TeacherImportForm.vue'),
                meta: {
                  title: '教师列表',
                  menu: false,
                  activeMenu: '/organization/people',
                },
              },
              {
                path: 'editStudent',
                name: 'editStudent',
                component: () =>
                  import('@/page/organization/people/StudentForm.vue'),
                meta: {
                  title: '学生列表',
                  menu: false,
                  activeMenu: '/organization/people',
                },
              },
            ],
          },
        ],
      },
      {
        path: '/ecology',
        component: Layout,
        redirect: '/ecology/list',
        meta: {
          title: '生态任务管理',
          icon: 'i-ri:external-link-fill',
        },
        children: [
          {
            path: 'list',
            component: () => import('@/page/ecology/list/index.vue'),
            meta: {
              title: '生态任务分类与积分',
            },
            children: [
              {
                path: 'edit',
                name: 'editEcology',
                component: () => import('@/page/ecology/list/EditForm.vue'),
                meta: {
                  title: '编辑',
                  menu: false,
                  activeMenu: '/ecology/list',
                },
              },
            ],
          },
        ],
      },
      {
        path: '/event',
        component: Layout,
        redirect: '/event/list',
        meta: {
          title: '活动管理',
          icon: 'ri:shield-keyhole-line',
        },
        children: [
          {
            path: 'list',
            component: () => import('@/page/event/list.vue'),
            meta: {
              title: '活动审核',
            },
          },
          {
            path: 'info',
            name: 'viewEvent',
            component: () => import('@/page/event/info.vue'),
            meta: {
              title: '活动详情',
              menu: false,
              activeMenu: '/event/list',
            },
          },
        ],
      },
      {
        path: '/xunzhang',
        component: Layout,
        redirect: '/xunzhang/list',
        meta: {
          title: '勋章管理',
          icon: 'i-ic:twotone-auto-awesome',
        },
        children: [
          {
            path: 'list',
            component: () => import('@/page/xunzhang/code/list.vue'),
            meta: {
              title: '勋章管理',
            },
            children: [
              {
                path: 'edit',
                name: 'editXunzhang',
                component: () => import('@/page/xunzhang/code/EditForm.vue'),
                meta: {
                  title: '编辑',
                  menu: false,
                  activeMenu: '/xunzhang/list',
                },
              },
            ],
          },
        ],
      },
      {
        path: '/thankLetter',
        component: Layout,
        redirect: '/thankLetter/list',
        meta: {
          title: '感谢信管理',
          icon: 'i-bx:bxs-component',
        },
        children: [
          {
            path: 'list',
            component: () => import('@/page/thankLetter/list/index.vue'),
            meta: {
              title: '感谢信管理',
            },
            children: [
              {
                path: 'edit',
                name: 'editThankLetter',
                component: () => import('@/page/thankLetter/list/EditForm.vue'),
                meta: {
                  title: '编辑',
                  menu: false,
                  activeMenu: '/thankLetter/list',
                },
              },
            ],
          },
          {
            path: 'get',
            component: () => import('@/page/thankLetter/list/get.vue'),
            meta: {
              title: '感谢信申领',
            },
          },
        ],
      },
      {
        path: '/data',
        component: Layout,
        redirect: '/data/list',
        meta: {
          title: '数据统计',
          icon: 'i-ic:round-tab',
        },
        children: [
          {
            path: 'list',
            component: () => import('@/page/data/list/index.vue'),
            meta: {
              title: '机构数据',
            },
          },
        ],
      },
      {
        path: '/complain',
        component: Layout,
        redirect: '/complain/list',
        meta: {
          title: '投诉管理',
          icon: 'i-cil:window-restore',
        },
        children: [
          {
            path: 'list',
            component: () => import('@/page/complain/list/index.vue'),
            meta: {
              title: '投诉管理',
            },
            children: [
              {
                path: 'info',
                name: 'infoComplain',
                component: () => import('@/page/complain/list/info.vue'),
                meta: {
                  title: '详情',
                  menu: false,
                  activeMenu: '/complain/list',
                },
              },
            ],
          },
        ],
      },
      {
        path: '/userMangerList',
        component: Layout,
        redirect: '/userMangerList/list',
        meta: {
          title: '用户管理',
          icon: 'i-clarity:plugin-outline-alerted',
        },
        children: [
          {
            path: 'list',
            component: () => import('@/page/userMangerList/user/list.vue'),
            meta: {
              title: '用户管理',
            },
            children: [
              {
                path: 'edit',
                name: 'editUser',
                component: () =>
                  import('@/page/userMangerList/user/EditForm.vue'),
                meta: {
                  title: '编辑',
                  menu: false,
                  activeMenu: '/userMangerList/list',
                },
              },
            ],
          },
        ],
      },
    ],
  },
  {
    meta: {
      title: '菜单2',
      icon: 'i-uim:box',
    },
    children: [
      {
        path: '/event2',
        component: Layout,
        redirect: '/event2/list',
        meta: {
          title: '活动管理',
          icon: 'ri:shield-keyhole-line',
        },
        children: [
          {
            path: 'list',
            component: () => import('@/page2/event/list.vue'),
            meta: {
              title: '生态主题活动',
            },
          },
          {
            path: 'info',
            name: 'viewEvent2',
            component: () => import('@/page2/event/info.vue'),
            meta: {
              title: '活动详情',
              menu: false,
              activeMenu: '/event2/list',
            },
          },
        ],
      },
      {
        path: '/order',
        component: Layout,
        redirect: '/order/list',
        meta: {
          title: '订单管理',
          icon: 'uim:box',
        },
        children: [
          {
            path: 'list',
            component: () => import('@/page2/order/list.vue'),
            meta: {
              title: '订单管理',
            },
          },
        ],
      },
      {
        path: '/green',
        component: Layout,
        redirect: '/green/list',
        meta: {
          title: '绿色档案管理',
          icon: 'i-ri:external-link-fill',
        },
        children: [
          {
            path: 'list',
            component: () => import('@/page2/green/list.vue'),
            meta: {
              title: '生态结果审核',
            },
            children: [
              {
                path: 'edit',
                name: 'editGreen',
                component: () => import('@/page2/green/info.vue'),
                meta: {
                  title: '编辑',
                  menu: false,
                  activeMenu: '/green/list',
                },
              },
            ],
          },
        ],
      },

      {
        path: '/userMangerList2',
        component: Layout,
        redirect: '/userMangerList2/teacher',
        meta: {
          title: '用户管理',
          icon: 'i-clarity:plugin-outline-alerted',
        },
        children: [
          {
            path: 'teacher',
            component: () => import('@/page2/userMangerList/teacher.vue'),
            meta: {
              title: '教师管理',
            },
          },
          {
            path: 'student',
            component: () => import('@/page2/userMangerList/student.vue'),
            meta: {
              title: '学生管理',
            },
            children: [
              {
                path: 'editStudent',
                name: 'editStudent2',
                component: () =>
                  import('@/page2/userMangerList/studentForm.vue'),
                meta: {
                  title: '编辑',
                  menu: false,
                  activeMenu: '/userMangerList2/student',
                },
              },
              {
                path: 'importStudent',
                name: 'importStudent',
                component: () =>
                  import('@/page2/userMangerList/studentImportForm.vue'),
                meta: {
                  title: '批量导入',
                  menu: false,
                  activeMenu: '/userMangerList2/student',
                },
              },
            ],
          },
          {
            path: 'nivite',
            component: () => import('@/page2/userMangerList/invite.vue'),
            meta: {
              title: '邀请学生',
            },
          },
        ],
      },
      // MultilevelMenuExample,
      // BreadcrumbExample,
      // KeepAliveExample,
      // TabExample,
      // ComponentExample,
      // IconExample,
      // FeatureExample,
      // PluginExample,
      // PermissionExample,
      // MockExample,
      // JsxExample,
      // ExternalLinkExample,
    ],
  },
];

const constantRoutesByFilesystem = generatedRoutes.filter(item => {
  return item.meta?.enabled !== false && item.meta?.constant === true;
});

const asyncRoutesByFilesystem = setupLayouts(
  generatedRoutes.filter(item => {
    return (
      item.meta?.enabled !== false &&
      item.meta?.constant !== true &&
      item.meta?.layout !== false
    );
  })
);

export {
  constantRoutes,
  systemRoutes,
  asyncRoutes,
  constantRoutesByFilesystem,
  asyncRoutesByFilesystem,
};
